//
//  BHCircleDetailServiceManager.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/6.
//

import UIKit

class BHCircleDetailServiceManager: NSObject {
    var circleId: String = ""
    var detailResponseForMainBlock:((Bool,String,BHCircleDetailInfoModel) -> Void)?
    var detailResponseForReviewBlock:((Bool,String,Int,[BHCircleReviewModel]) -> Void)?
    var reviewOrReplyResultActBlock:((Bool,String) -> Void)?
    var deleteReviewOrReplyBlock:((Bool,String) -> Void)?
    var deleteCircleOrReplyBlock:((Bool,String) -> Void)?

    
    // 获取详情内容
    func loadDetailService()  {
        let detailManager = BHHTTPClient.init()
        detailManager.get(url: String.circle_detail_url, paramters: ["id":self.circleId,"longitude":BHLocationManager.shared.longitude,"latitude":BHLocationManager.shared.latitude]) { (success, errorMsg, responseDic) in
          
            let tempModel = BHCircleDetailInfoModel.init()
            if success == true {
                let resultDic = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                tempModel.parseDic(resultDic)
            }
            self.detailResponseForMainBlock?(success,errorMsg,tempModel)
        }

    }
    
    // 删除圈子
    func deleteCircleService(circleId: String)  {
        let manager = BHHTTPClient.init()
        manager.delete(url: String.circle_delete_url, paramters: ["id":circleId]) { (success, errorMsg, responseDic) in
            self.deleteCircleOrReplyBlock?(success,errorMsg)
        }
    }
    
    
    
    // 获取评论
    func loadReviewService(page: Int) {
        let manager = BHHTTPClient.init()
        manager.get(url: String.circle_review_list_url, paramters: ["pageSize":"10","pageNo":page,"circleId":self.circleId]) { (success, errorMsg, responseDic) in
            var temp: [BHCircleReviewModel] = []
            var total: Int = 0
            
            if success == true{
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                total = BHJsonTool.decodeIntFromDic(dic: responseDic, key: "total")
                
                let record = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
                
                for item in record {
                    let model = BHCircleReviewModel.init()
                    model.parseDic(item as! [String:Any])
                    temp.append(model)
                }

            }
            self.detailResponseForReviewBlock?(success,errorMsg,total,temp)
        }
    }
    
    // 发表评论
    func reviewCircleService(content: String) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.circle_reviewDyn_url, paramters: ["content":content,"circleId":self.circleId]) { (success, errorMsg, responseDic) in
            self.reviewOrReplyResultActBlock?(success,errorMsg)
        }
    }
    
    // 回复评论
    func replyReviewService(reviewId: String,content: String) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.circle_replyReview_url, paramters: ["content":content,"id":reviewId]) { (success, errorMsg, responseDic) in
            self.reviewOrReplyResultActBlock?(success,errorMsg)
        }
    }
    
    // 删除评论
    func deleteReviewService(reviewOrReplyId: String)  {
        let manager = BHHTTPClient.init()
        manager.delete(url: String.circle_review_delete_url, paramters: ["id":reviewOrReplyId]) { (success, errorMsg, responseDic) in
            self.deleteReviewOrReplyBlock?(success,errorMsg)
        }
    }
}
